package com.common.displayObjects
{
	import com.common.utils.HashMap;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class Component extends Sprite
	{
		private var hasmapListener:HashMap;
		
		public function Component()
		{
			super();
			createChildren();
		}
		
		protected function createChildren():void
		{
			hasmapListener = new HashMap();
		}
		
		/**
		 * 重写事件侦听的添加
		 * @param type
		 * @param listener
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 * 
		 */		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			if (hasmapListener.get(type) && hasmapListener.get(type).listener == listener)
			{
				return;
			}else
			{
				super.addEventListener(type, listener, useCapture, priority, useWeakReference);
				hasmapListener.put(type, {listener:listener, useCapture:useCapture, priority:priority, useWeakReference:useWeakReference});
			}
		}
		
		/**
		 * 移除事件 和 在哈希表的事件记录
		 * @param type
		 * @param listener
		 * @param useCapture
		 * 
		 */		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			if (hasmapListener.containsKey(type))
			{
				super.removeEventListener(type, listener, useCapture);
				hasmapListener.remove(type);
			}
		}
		/**
		 *移除全部事件 
		 * 
		 */		
		public function removeAllListener():void
		{
			for (var i:* in hasmapListener)
			{
				var key:* = i;
				var o:Object = hasmapListener[key];
				super.removeEventListener(key, o.listener, o.useCapture);
			}
			hasmapListener.clear();
		}
		
		/**
		 *	销毁 
		 * 
		 */		
		public function dispose():void
		{
			var disObj:*;
			removeAllListener();
			while(numChildren > 0)
			{
				disObj = getChildAt(0);
				if (disObj is Component)
				{
					(disObj as Component).dispose();
				}else if (disObj is Shape)
				{
					(disObj as Shape).graphics.clear();
				}else if (disObj is Bitmap)
				{
					(disObj as Bitmap).bitmapData = null;
				}else if (disObj is MovieClip)
				{
					(disObj as MovieClip).stop();
				}
				removeChild(disObj);
				disObj = null;
			}
		}
	}
}